#include "mainwindow.h"
#include <QDebug>
#include <QGraphicsOpacityEffect>
#include <QHBoxLayout>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent)
    : DBlurEffectWidget(parent)
{


    showFullScreen();
    setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);//关闭任务栏图标并总在最前
    setWindowOpacity(0);
    QTimer *timer=new QTimer;
    timer->setInterval(10);
    timer->start();
    connect(timer,&QTimer::timeout,[=](){
        float opacity=float(windowOpacity());
        opacity+=float(0.05);
        setWindowOpacity(qreal(opacity));
        if(double(windowOpacity())>=1.0){
           timer->stop();
           delete timer;
        }
    });
    setMaskAlpha(150);
    setMaskColor(QColor(0,0,0));

    btn_shutdown->setAutoFillBackground(true);
    btn_shutdown->setIconSize(QSize(50, 50));
    btn_shutdown->setText("关机");
    btn_shutdown->setIcon(QIcon(":/icon/shutdown.svg"));
    btn_shutdown->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    btn_shutdown->setFixedSize(100, 120);
    btn_shutdown->installEventFilter(this);

    connect(btn_shutdown,&QToolButton::clicked,[=](){
        close("init 0");
    });

    btn_reboot->setAutoFillBackground(true);
    btn_reboot->setIconSize(QSize(50, 50));
    btn_reboot->setText("重启");
    btn_reboot->setIcon(QIcon(":/icon/reboot.svg"));
    btn_reboot->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    btn_reboot->setFixedSize(100, 120);
    btn_reboot->installEventFilter(this);
    connect(btn_reboot,&QToolButton::clicked,[=](){
        close("init 6");
    });

    btn_suspend->setAutoFillBackground(true);
    btn_suspend->setIconSize(QSize(50, 50));
    btn_suspend->setText("待机");
    btn_suspend->setIcon(QIcon(":/icon/suspend.svg"));
    btn_suspend->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    btn_suspend->setFixedSize(100, 120);
    btn_suspend->installEventFilter(this);
    connect(btn_suspend,&QToolButton::clicked,[=](){
        close("dbus-send --print-reply --dest=com.deepin.SessionManager /com/deepin/SessionManager  com.deepin.SessionManager.RequestSuspend");
    });

    btn_lock->setAutoFillBackground(true);
    btn_lock->setIconSize(QSize(50, 50));
    btn_lock->setText("锁定");
    btn_lock->setIcon(QIcon(":/icon/lock.svg"));
    btn_lock->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    btn_lock->setFixedSize(100, 120);
    btn_lock->installEventFilter(this);
    connect(btn_lock,&QToolButton::clicked,[=](){
        close("dbus-send --print-reply --dest=com.deepin.SessionManager /com/deepin/SessionManager  com.deepin.SessionManager.RequestLock");
    });

    btn_logout->setAutoFillBackground(true);
    btn_logout->setIconSize(QSize(50, 50));
    btn_logout->setText("注销");
    btn_logout->setIcon(QIcon(":/icon/logout.svg"));
    btn_logout->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    btn_logout->setFixedSize(100, 120);
    btn_logout->installEventFilter(this);
    connect(btn_logout,&QToolButton::clicked,[=](){
        close("dbus-send --print-reply --dest=com.deepin.SessionManager /com/deepin/SessionManager  com.deepin.SessionManager.RequestLogout");
    });

    QHBoxLayout *layout=new QHBoxLayout;
    layout->addStretch();
    layout->addWidget(btn_shutdown);
    layout->addWidget(btn_reboot);
    layout->addWidget(btn_suspend);
    layout->addWidget(btn_lock);
    layout->addWidget(btn_logout);
    layout->addStretch();
    setLayout(layout);
    layout->setSpacing(40);
    QString stylesheet =
                "QToolButton { color:white; font-size:16px; background:transparent ; }"
                "QToolButton::focus { background:rgba(0,0,0,80); }"
                "QToolButton::hover { color:rgba(255,255,255,255); background:rgba(255,255,255,50); }"
                "QToolButton::pressed {color:white; }";
    setStyleSheet(stylesheet);
    setFocus();
}
MainWindow::~MainWindow()
{


}

void MainWindow::close(QString cmd)
{
    QTimer *timer=new QTimer;
    timer->setInterval(10);
    timer->start();
    connect(timer,&QTimer::timeout,[=](){
        float opacity=float(windowOpacity());
        opacity-=float(0.05);
        setWindowOpacity(qreal(opacity));
        if(double(windowOpacity())<=0){
           timer->stop();
           delete timer;
           if(!cmd.isEmpty()){
               QProcess run;
               run.start(cmd);
               run.waitForFinished();
           }
           delete this;
        }
    });
}

void MainWindow::mousePressEvent(QMouseEvent *)
{
    close("");
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{

    if(event->key()==Qt::Key_Escape || event->key()==Qt::Key_Space){
        close("");
    }
}
